package com.rachel.testjetpack;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 优雅的检测应用程序前后台状态，监听应用前后台切换
 *
 * 很多业务中我们都需要监听应用前后台的状态 ，比如在前台的时候开始缓存任务，在后台的时候停止缓存任务，其实方法有很多，比如
 * 使用我们上面所说的方法，监听BaseActivity，但是都不够优雅，如果我们想监听所有Activty的生命周期就要使用ActivityLifecycleCallbacks
 * 创建ForegroundCallbacks，在Application中进行注册:
 */
public class ForegroundCallbacks implements Application.ActivityLifecycleCallbacks {
    private Handler handler = new Handler();

    public interface Listener {
        /**
         * 在前台
         */
        public void onBecameForeground();

        /**
         * 在后台
         */
        public void onBecameBackground();
    }

    private Runnable runnable;

    /**
     * 是否执行过onResumed
     */
    private boolean onResumed = false;

    /**
     * 是否执行过onPaused
     */
    private boolean onPaused = true;
    private Listener listener;

    /**
     * 初始化foregroundCallbacks
     *
     * @param appApplication application
     */
    public static ForegroundCallbacks init(AppApplication appApplication) {

        ForegroundCallbacks instance = new ForegroundCallbacks();
        appApplication.registerActivityLifecycleCallbacks(instance);
        return instance;
    }

    private static final String TAG = "ForegroundCallbacks";

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
        Log.d(TAG, "onActivityCreated: " + activity.getComponentName());
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {
        Log.d(TAG, "onActivityStarted: " + activity.getComponentName());
    }


    @Override
    public void onActivityStopped(@NonNull Activity activity) {
        Log.d(TAG, "onActivityStopped: " + activity.getComponentName());
    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
        Log.d(TAG, "onActivitySaveInstanceState: " + activity.getComponentName());
    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {
        Log.d(TAG, "onActivityDestroyed: " + activity.getComponentName());
    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {
        Log.d(TAG, "onActivityResumed: " + activity.getComponentName());
        onPaused = false;
        if (runnable != null) {
            handler.removeCallbacks(runnable);
        }
        handler.postDelayed(runnable = new Runnable() {
            @Override
            public void run() {
                if (!onResumed) {
                    listener.onBecameForeground();
                    onResumed = true;
                }
            }
        }, 600);

    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {
        Log.d(TAG, "onActivityPaused: " + activity.getComponentName());

        onPaused = true;
        if (runnable != null) {
            handler.removeCallbacks(runnable);
        }

        handler.postDelayed(runnable = new Runnable() {
            @Override
            public void run() {
                if (onResumed && onPaused) {
                    onResumed = false;
                    listener.onBecameBackground();
                }
            }
        }, 600);


    }

    public void addListener(Listener listener) {
        this.listener = listener;

    }
}